package com.baoyi.pooke;

import java.util.List;

import net.youmi.android.AdManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyi.content.content;
import com.baoyi.data.MessageList;
import com.baoyi.doamin.Comments;
import com.baoyi.doamin.ImageItem;
import com.baoyi.service.CommentsServiceImpl;
import com.baoyi.service.ImageItemService;
import com.baoyi.widget.MyListView;
import com.baoyi.widget.RemoteImageView;

public class ImageViewUI extends Activity {
	private Integer id;
	ImageItem item;
	static {
		AdManager.init(content.id, content.pwd, content.time, content.con,
				content.banben);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		id = (Integer) getIntent().getExtras().get("id");
		setContentView(R.layout.imageview);
		imageView = (RemoteImageView) findViewById(R.id.remoteImageView);
		imageView.setDefaultImage(R.drawable.loading02);
		bishitextView = (TextView) findViewById(R.id.bishitext);
		geilitextView = (TextView) findViewById(R.id.renqitext);
		gaoxiaotextView = (TextView) findViewById(R.id.gaoxiaotext);
		commentsize = (TextView) findViewById(R.id.commentsize);
		geilitextView = (TextView) findViewById(R.id.renqitext);
		xinqitextView = (TextView) findViewById(R.id.xinqitext);
		imageviewcontent=(TextView) findViewById(R.id.imageviewcontent);
		myListView = (MyListView) findViewById(R.id.commentslistdatas);
		bishibutton = (ImageButton) findViewById(R.id.bishibutton);
		renqibutton = (ImageButton) findViewById(R.id.renqibutton);
		gaoxiaobutton = (ImageButton) findViewById(R.id.gaoxiaobutton);
		xinqibutton = (ImageButton) findViewById(R.id.xinqibutton);
		button2 = (ImageButton) findViewById(R.id.button2);
		woyaoping = (ImageButton) findViewById(R.id.woyaoping);

		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Intent item = new Intent(MainUI.this, LocGalleryItem.class);
				Intent item = new Intent(ImageViewUI.this, PaiKuUI.class);
				startActivity(item);

			}
		});
		button3 = (ImageButton) findViewById(R.id.button3);
		button3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent item = new Intent(ImageViewUI.this, CamActivity.class);
				startActivity(item);

			}
		});

		item = ImageItemService.findById(id);
		if (null != item) {
			try {
				String u = content.server + item.getBigimageurl();
				imageView.setImageUrl(u);
				imageviewcontent.setText(item.getContents());
				bishitextView.setText(item.getBishi().toString());
				geilitextView.setText(item.getGeili().toString());
				xinqitextView.setText(item.getXinqi().toString());
				gaoxiaotextView.setText(item.getGaoxiao().toString());
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		bishibutton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ImageItemService.bishi(id);
				try {
					String message = MessageList.getMessage();
					if (BaoyiApplication.getInstance().isonline()) {

						message += "本图鄙视指数:" + item.getBishi() + "";
					}
					Toast.makeText(ImageViewUI.this, message,
							Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
				}

			}
		});

		renqibutton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ImageItemService.ding(id);
				try {
					String message = MessageList.getMessage();
					if (BaoyiApplication.getInstance().isonline()) {

						message += "本图给力指数:" + item.getGeili() + "";
					}
					Toast.makeText(ImageViewUI.this, message,
							Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
				}
			}
		});

		xinqibutton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ImageItemService.xinqi(id);
				try {
					String message = MessageList.getMessage();
					if (BaoyiApplication.getInstance().isonline()) {

						message += "本图新奇指数:" + item.getXinqi() + "";
					}
					Toast.makeText(ImageViewUI.this, message,
							Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
				}
			}
		});
		gaoxiaobutton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ImageItemService.gaoxiao(id);
				try {
					String message = MessageList.getMessage();
					if (BaoyiApplication.getInstance().isonline()) {

						message += "本图搞笑指数:" + item.getGaoxiao() + "";
					}
					Toast.makeText(ImageViewUI.this, message,
							Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
				}
			}
		});
		woyaoping.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				buildDialog1(ImageViewUI.this).show();
			}
		});
		CommentsServiceImpl service = new CommentsServiceImpl();
		List<Comments> comments = service.findbypid(id);
		myListView.setDatas(comments);
		if (null != comments) {
			String msg = "已有" + comments.size() + "评论";
			commentsize.setText(msg);
		}
		// CommentsAdapter adapter=new CommentsAdapter(this);
		// listView.setAdapter(adapter);
		// listView.setDividerHeight(0);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	private Dialog buildDialog1(Context context) {
		LayoutInflater inflater = LayoutInflater.from(this);
		final View textEntryView = inflater.inflate(R.layout.discussdailog,
				null);
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("我要评论");
		builder.setView(textEntryView);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				EditText content = (EditText) textEntryView
						.findViewById(R.id.editText1);

				String ss = content.getText().toString();
				ss = ss.trim();
				if (ss != null && ss.length() > 2) {
					Comments commentsitem = new Comments();
					commentsitem.setContents(ss);
					commentsitem.setId(1);
					commentsitem.setIp("");
					commentsitem.setMacaddress(BaoyiApplication.getInstance()
							.getLocalMacAddress());
					commentsitem.setPubtime("");
					commentsitem.setWorkitemid(item.getId());
					CommentsServiceImpl commentsServiceImpl = new CommentsServiceImpl();
					commentsServiceImpl.add(commentsitem);
					Intent item1 = new Intent(ImageViewUI.this,
							ImageViewUI.class);
					item1.putExtra("id", item.getId());
					startActivity(item1);
					Toast.makeText(ImageViewUI.this, "添加评论成功",
							Toast.LENGTH_SHORT).show();
					finish();
				} else {
					Toast.makeText(ImageViewUI.this, "请填写评论内容！",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

			}
		});
		return builder.create();
	}

	private MyListView myListView;
	private TextView imageviewcontent;
	private TextView bishitextView;
	private TextView geilitextView;
	private TextView gaoxiaotextView;
	private TextView xinqitextView;
	private TextView commentsize;

	private ImageButton renqibutton;
	private ImageButton bishibutton;
	private ImageButton gaoxiaobutton;
	private ImageButton xinqibutton;
	private RemoteImageView imageView;
	private ImageButton woyaoping;
	private ImageButton button2;
	private ImageButton button3;
}
